package com.atguigu.mybatisplus;

public class Test2 {
    public static void main(String[] args) {
        Business2 business = new Business2();
        new Thread(()->{
            try{
                for (int i = 0;i<3;i++){
                    business.decr();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        },"父线程1").start();
        new Thread(()->{
            try{
                for (int i = 0;i<3;i++){
                    business.incr();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        },"子线程1").start();
    }
}
class Business2{
    public int num = 0;
    public int sum = 1;
    public int aum = 0;
    public synchronized void incr() throws Exception{
        //判断
        if(num!=0){
            this.wait();
        }
        //干活
        System.out.println(Thread.currentThread().getName()+":\t"+sum);
        num++;
        //通知
        sum++;
        this.notifyAll();
    }

    public synchronized void decr() throws Exception {
        if(num==0){
            this.wait();

        }
        char c = (char) (aum+65);
        System.out.println(Thread.currentThread().getName()+":\t"+c);
        num--;
        aum++;
        this.notifyAll();
    }
}
